/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package game_interface;

import game_logic.Board;
import game_logic.Square;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;

/**
 *
 * @author Danilo
 */
public class BoardPanel extends JPanel implements MouseInputListener {

    private static final Color[] colors = {Color.GREEN, Color.RED, Color.BLUE,
        Color.YELLOW};
    private static final int SQUARE_SIZE = 40;
    private static int[][] positions = {
        {-1, -1, -1, -1, -1, -1, 11, 12, 13, -1, -1, -1, -1, -1, -1},
        {-1, -1, -1, -1, -1, -1, 10, -1, 14, -1, -1, -1, -1, -1, -1},
        {-1, -1, -1, -1, -1, -1, 9, -1, 15, -1, -1, -1, -1, -1, -1},
        {-1, -1, -1, -1, -1, -1, 8, -1, 16, -1, -1, -1, -1, -1, -1},
        {-1, -1, -1, -1, -1, -1, 7, -1, 17, -1, -1, -1, -1, -1, -1},
        {-1, -1, -1, -1, -1, -1, 6, -1, 18, -1, -1, -1, -1, -1, -1},
        {0, 1, 2, 3, 4, 5, -1, -1, -1, 19, 20, 21, 22, 23, 24},
        {51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 25},
        {50, 49, 48, 47, 46, 45, -1, -1, -1, 31, 30, 29, 28, 27, 26},
        {-1, -1, -1, -1, -1, -1, 44, -1, 32, -1, -1, -1, -1, -1, -1},
        {-1, -1, -1, -1, -1, -1, 43, -1, 33, -1, -1, -1, -1, -1, -1},
        {-1, -1, -1, -1, -1, -1, 42, -1, 34, -1, -1, -1, -1, -1, -1},
        {-1, -1, -1, -1, -1, -1, 41, -1, 35, -1, -1, -1, -1, -1, -1},
        {-1, -1, -1, -1, -1, -1, 40, -1, 36, -1, -1, -1, -1, -1, -1},
        {-1, -1, -1, -1, -1, -1, 39, 38, 37, -1, -1, -1, -1, -1, -1},};
    
    private static Map<Integer, Point> mapPositions;

    static {
        mapPositions = new HashMap<Integer, Point>();

        for (int i = 0; i < positions.length; i++) {
            for (int j = 0; j < positions.length; j++) {
                int val = positions[i][j];
                if (val != -1) {
                    mapPositions.put(val, new Point(i, j));
                }
            }
        }
    }
    private Board board;
    private List<PawnMapper> pawns;
    private List<SquareMapper> squares;
    private SquareMapper sqSelected;

    public BoardPanel(Board board) {
        this.board = board;

        initializeListSquare();

        addMouseListener(this);
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2D = (Graphics2D) g;



        g2D.setColor(Color.WHITE);
        paintBoard(g2D);
        paintPawns(g2D);


        //Pawn p = new Pawn(board, game_logic.Color.GREEN, 1);
        //PawnMapper pm = new PawnMapper(p);
        // ponto = mapPositions.get(1);

        //paintPawn(g2D, pm, ponto);

    }

    private void paintBoard(Graphics2D g) {
        //List<Square> list = board.getPath();

        for (SquareMapper sqm : squares) {
            drawSquare(g, sqm);
        }
    }

    private void paintPawns( Graphics2D g ) {

        for( PawnMapper pm : pawns ){
            int position = pm.getPawn().getPosition();
            if( position != -1 ){
                Point p = mapPositions.get(position);
                paintPawn( g, pm, p);
            }
            //Point p = mapPositions.get(pm.getPawn().getPosition());
            
        }

    }

    private void paintPawn(Graphics2D g, PawnMapper pm, Point p) {
        Color c = pm.getPawn().getColor().getAwtColor();

        g.setColor(c);

        double x = p.getY() * SQUARE_SIZE;
        double y = p.getX() * SQUARE_SIZE;
        double w = SQUARE_SIZE;


        double x0 = x + (w / 8.0) * 3;
        double y0 = y;

        g.fill(new Ellipse2D.Double(x0, y0, w / 4, w / 4));

        g.setPaint(Color.BLACK);
        g.setStroke(new BasicStroke(1.0f));

        g.draw(new Ellipse2D.Double(x0, y0, w / 4, w / 4));

        double x1 = x + (w / 8) * 3;
        double y1 = y + (w / 4);
        double x2 = x + (w / 8) * 5;
        double y2 = y + (w / 4);
        double x3 = x + (w / 4);
        double y3 = y + (w);
        double x4 = x + (w / 4) * 3;
        double y4 = y + w;

        GeneralPath gp = new GeneralPath();

        gp.moveTo(x1, y1);
        gp.lineTo(x2, y2);
        gp.lineTo(x4, y4);
        gp.lineTo(x3, y3);

        gp.closePath();

        g.setColor(c);
        g.fill(gp);

        g.setPaint(Color.BLACK);
        g.setStroke(new BasicStroke(1.0f));

        g.draw(gp);
    }

    private void drawSquare(Graphics2D g, SquareMapper sqm) {
        Color c = sqm.getSquare().getColor().getAwtColor();

        Point p = mapPositions.get(sqm.getSquare().getPosition());


        g.setColor(c);
        g.fillRect((int) (p.getY() * SQUARE_SIZE), (int) (p.getX() * SQUARE_SIZE), SQUARE_SIZE, SQUARE_SIZE);

        g.setPaint(Color.BLACK);
        if (sqm.isSelected()) {
            g.setPaint(Color.RED);
        }
        g.setStroke(new BasicStroke(2.0f));
        g.draw(new Rectangle2D.Double((p.getY() * SQUARE_SIZE), (p.getX() * SQUARE_SIZE), SQUARE_SIZE, SQUARE_SIZE));

        //g.drawString(sq.getPosition() + "", (int) (p.getY() * SQUARE_SIZE  + SQUARE_SIZE/3), (int) (p.getX() * SQUARE_SIZE  + SQUARE_SIZE/2));
    }

    public static void main(String[] args) {
        MainForm.main(args);
    }

    private void initializeListSquare() {
        squares = new ArrayList<SquareMapper>();
        pawns = new ArrayList<PawnMapper>();

        List<Square> sqs = board.getPath();

        for (Square sq : sqs) {

            Point p = mapPositions.get(sq.getPosition());
            Rectangle2D.Double rec = new Rectangle2D.Double((p.getY() * SQUARE_SIZE), (p.getX() * SQUARE_SIZE), SQUARE_SIZE, SQUARE_SIZE);

            SquareMapper sqm = new SquareMapper(sq, new Area(rec));
            squares.add(sqm);


        }
    }

    public void mouseClicked(MouseEvent e) {
        if (sqSelected != null) {
            sqSelected.setSelected(false);
        }

        for (SquareMapper sm : squares) {
            if (sm.getArea().contains(e.getX(), e.getY())) {
                System.out.println(sm.getSquare().getPosition());

                sqSelected = sm;
                sqSelected.setSelected(true);
            }
        }

        repaint();

    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseDragged(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {
    }
}
